package com.alai04.android.preference;

import com.alai04.android.scoreboard.R;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.TypedArray;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class SeekBarPreference extends Preference implements
		OnSeekBarChangeListener {

	public final static int defMax = 100;
	public final static int defMin = 0;
	public final static int defInterval = 5;

	private int mMax;
	private int mMin;
	private int mInterval;
	private int mProgress = 0;
	private TextView monitorBox;

	public SeekBarPreference(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		setWidgetLayoutResource(R.layout.preference_widget_seekbar);
	}

	public SeekBarPreference(Context context, AttributeSet attrs) {
		this(context, attrs, R.attr.SeekBarPreferenceStyle);
	}

	public SeekBarPreference(Context context) {
		super(context, null);
	}

	public void setLimit(int min, int max) {
		if (max > min) {
			mMax = max;
			mMin = min;
			notifyChanged();
		}
	}

	public int getMax() {
		return mMax;
	}

	public int getMin() {
		return mMin;
	}

	public void setInterval(int interval) {
		mInterval = interval;
		notifyChanged();
	}

	public int getInterval() {
		return mInterval;
	}

	@Override
	protected void onBindView(View view) {
		super.onBindView(view);
		final SeekBar seekBar = (SeekBar) view.findViewById(R.id.seekbar);
		if (seekBar != null) {
			seekBar.setMax(mMax - mMin);
			seekBar.setProgress((int) mProgress - mMin);
			seekBar.setOnSeekBarChangeListener(this);
		}

		monitorBox = (TextView) view.findViewById(R.id.seekbar_value);
		if (monitorBox != null) {
			monitorBox.setText(mProgress + "");
		}
	}

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		mProgress = Math.round(((float) progress) / mInterval) * mInterval
				+ mMin;
		if (!callChangeListener(progress)) {
			return;
		}
		updatePreference(mProgress);
		notifyChanged();
	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {
	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
	}

	@Override
	protected Object onGetDefaultValue(TypedArray ta, int index) {
		int dValue = (int) ta.getInt(index, defMin);
		return validateValue(dValue);
	}

	@Override
	protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
		int temp = restoreValue ? getPersistedInt(0) : (Integer) defaultValue;
		if (!restoreValue)
			persistInt(temp);
		mProgress = temp;
	}

	private int validateValue(int value) {
		if (value < mMin)
			value = mMin;
		else if (value > mMax)
			value = mMax;
		value = Math.round(((float) (value - mMin)) / mInterval) * mInterval
				+ mMin;
		return value;
	}

	private void updatePreference(int newValue) {
		SharedPreferences.Editor editor = getEditor();
		editor.putInt(getKey(), newValue);
		editor.commit();
	}

}
